package edu.nf.project.webs.accountsAndPermissions;

import edu.nf.project.entitys.dto.accountsAndPermissions.UserRoleDTO;
import edu.nf.project.entitys.entity.accountsAndPermissions.convention.Roles;
import edu.nf.project.services.accountsAndPermissions.RolesService;
import edu.nf.project.services.exception.derivedAnomalies.RolesException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/roles")
public class RolesController extends BaseController {
    @Autowired
    private RolesService rolesService;

    /**
     * 获取全部角色信息
     */
    @GetMapping
    public ResultVO<List<Roles>> getAllRoles() {
        try {
            List<Roles> roles = rolesService.getAllRoles();
            return success(roles);
        } catch (Exception e) {
            throw new RolesException(20000, "获取角色列表失败");
        }
    }

    /**
     * 新增角色
     * @param roles 角色信息
     * @return 是否成功
     */
    @PostMapping("/add")
    public ResultVO<Boolean> addRole(@RequestBody Roles roles) {
        try {
            boolean result = rolesService.addRoles(roles);
            if (result) {
                return success(true);
            } else {
                throw new RolesException(20001, "新增角色失败");
            }
        } catch (Exception e) {
            if (e instanceof RolesException) {
                throw e;
            }
            throw new RolesException(20000, "系统错误，新增角色失败");
        }
    }

    /**
     * 删除角色
     * @param rId 角色ID
     * @return 是否成功
     */
    @DeleteMapping("/del/{rId}")
    public ResultVO<Boolean> deleteRole(@PathVariable Integer rId) {
        try {
            boolean result = rolesService.delRoles(rId);
            if (result) {
                return success(true);
            } else {
                throw new RolesException(20002, "删除角色失败");
            }
        } catch (Exception e) {
            if (e instanceof RolesException) {
                throw e;
            }
            throw new RolesException(20000, "系统错误，删除角色失败");
        }
    }

    /**
     * 修改角色
     * @param rId 角色ID
     * @param roles 角色信息
     * @return 是否成功
     */
    @PostMapping("/upd/{rId}")
    public ResultVO<Boolean> updRoles(@PathVariable Integer rId, @RequestBody Roles roles) {
        try {
            // 将路径中的rId设置到roles对象中
            roles.setRId(rId);
            boolean result = rolesService.updRoles(roles);
            if (result) {
                return success(true);
            } else {
                throw new RolesException(20003, "修改角色失败，可能角色ID不存在或没有进行任何修改");
            }
        } catch (Exception e) {
            if (e instanceof RolesException) {
                throw e;
            }
            throw new RolesException(20000, "系统错误，修改角色失败");
        }
    }

    /**
     * 获取全部用户ID与对应角色名的集合
     * @return 用户ID与角色名集合
     */
    @GetMapping("/user-roles")
    public ResultVO<List<UserRoleDTO>> getAllUserRoles() {
        try {
            List<UserRoleDTO> userRoles = rolesService.getAllUserRoles();
            return success(userRoles);
        } catch (Exception e) {
            throw new RolesException(20004, "获取用户角色列表失败");
        }
    }

    /**
     * 给用户分配角色
     * @return 是否成功
     */
    @PostMapping("/giveRoles")
    public ResultVO<Boolean> rolesForUser(Integer uId, Integer rId) {
        try {
            boolean flag = rolesService.rolesForUser(uId, rId);
            return success(flag);
        } catch (Exception e) {
            System.out.println(e);
            throw new RolesException(20005, "角色分配失败");
        }
    }

    /**
     * 删除用户的角色
     * @return 是否成功
     */
    @PostMapping("/delRoles")
    public ResultVO<Boolean> delUsersRoles(Integer uId, Integer rId) {
        try {
            boolean flag = rolesService.delUsersRoles(uId, rId);
            return success(flag);
        } catch (Exception e) {
            System.out.println(e);
            throw new RolesException(20006, "角色删除失败");
        }
    }
}
